package com.valuegroup.smarttodo.ui.todo.rethink;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.model.Rethink;
import com.valuegroup.smarttodo.data.vo.AppCustomCfg;
import com.valuegroup.smarttodo.data.vo.BranchVo;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddActivity;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddEditText;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddInputCheckRule;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QuickAddEditorCallback;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QuickAddHolder;
import com.valuegroup.smarttodo.util.AlertUtils;
import com.valuegroup.smarttodo.util.Cfg;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 反思明细
 */
public class RethinkBranchAdapter extends RecyclerView.Adapter {

    private static final String TAG = RethinkBranchAdapter.class.getSimpleName();

    private List<Rethink> itemList;
    private Context context;
    private AppCustomCfg cfg;
    private CallParent callParent;

    public RethinkBranchAdapter(Long pid, Context context, CallParent callParent) {
        this.context = context;
        this.itemList = ServiceFactory.getInstance().getRethinkService().findRethink(pid);
        this.cfg = Cfg.getCfg(context);
        this.callParent = callParent;
    }

    public void addItem(int position, Rethink item) {
        Rethink rethink = ServiceFactory.getInstance().getRethinkService().getDefaultRethink();
        rethink.setTitle(item.getTitle());
        rethink.setPid(item.getPid());
        ServiceFactory.getInstance().getRethinkService().getRethinkDao().save(rethink);

        itemList.add(position, rethink);
        notifyItemInserted(position);
    }

    public void refreshListData(Long pid) {
        int previousSize = itemList.size();
        this.itemList.clear();
        notifyItemRangeRemoved(0, previousSize);
        this.itemList.addAll(ServiceFactory.getInstance().getRethinkService().findRethink(pid));
        notifyItemRangeInserted(0, getItemCount());
        this.cfg = Cfg.getCfg(context);
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_rethink_child, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder v = (ItemViewHolder) holder;
        v.initData(itemList.get(position));
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.labTitle)
        TextView labTitle;
        @BindView(R.id.btnDel)
        ImageView btnDel;

        private Rethink data;

        public ItemViewHolder(final View v) {
            super(v);
            ButterKnife.bind(this, v);
        }

        public void initData(Rethink data) {
            this.data = data;
            labTitle.setText(data.getTitle());
            btnDel.setVisibility(cfg.isRethinkEditModel() ? View.VISIBLE : View.GONE);
        }

        @OnClick({R.id.labTitle, R.id.btnDel})
        public void onViewClicked(View v) {
            switch (v.getId()) {
                case R.id.labTitle:

                    if (!cfg.isRethinkEditModel()) {
                        break;
                    }

                    QucikAddActivity.openEditorNoRepeat(
                            context,
                            new QuickAddEditorCallback() {
                                @Override
                                public void onCancel() {
                                }

                                @Override
                                public void onSubmit(String content) {
                                    if (TextUtils.isEmpty(content)) {
                                        return;
                                    }
                                    data.setTitle(content);
                                    ServiceFactory.getInstance().getRethinkService().updateTitle(data.getId(), content);
                                    notifyItemChanged(getAdapterPosition(), data);
                                }

                                @Override
                                public void onAttached(final ViewGroup rootView) {
                                    QucikAddEditText etContent = rootView.findViewById(R.id.quick_add_task_content_edt);
                                    etContent.setText(data.getTitle());
                                }
                            },
                            new QuickAddHolder(QuickAddHolder.QuickAddType.RETHINK),
                            new QucikAddInputCheckRule(120, 2));

                    break;
                case R.id.btnDel:

                    AlertUtils.delOrderDialog(context,
                            String.format(context.getResources().getString(R.string.alt_del), data.getTitle()),
                            (dialog, which) -> {
                                ServiceFactory.getInstance().getRethinkService().delRethink(data.getId());
                                //itemList.remove(getAdapterPosition());
                                //notifyItemRemoved(getAdapterPosition());
                                callParent.refreshView();
                            }
                    );
                    break;
            }
        }
    }

    public interface CallParent {
        void refreshView();
    }

}
